home *** CD-ROM | disk | FTP | other *** search
/ EDUCORP 8 / Educorp2Compilation.sit / educorp2 / Demos / Aztec Source Level Debugger / demo / count.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-06-22  |  3.4 KB  |  124 lines

  1. /* Program count.c originally distributed by Microsoft to demonstrate Codeview */
  2. /* for IBM PCs and compatibles.  Released by Microsoft into the public domain. */
  3. #include <stdio.h>
  4. #include <ctype.h>
  5.  
  6. #define        BUFFSIZE        512     /* Small buffer for debugging   */
  7. #define TRUE   1
  8. #define FALSE  0
  9. #define        FACTOR  1.1     /* Vowels per syllable in typical file  */
  10.  
  11. /* Conditional operator prevents divide by zero        */
  12.  
  13. #define WPS    (float)words / (sentences ? sentences : 1)
  14. #define LPW    (float)letters / (words ? words : 1)
  15. #define SPW    (vowels * FACTOR) / (words ? words : 1)
  16.  
  17. int    bytes = 0, characters = 0, words = 0, lines = 0;
  18. int    letters = 0, vowels = 0, sentences = 0;
  19.  
  20. char   buffer[BUFFSIZE];
  21.  
  22. main()
  23. {
  24.        FILE    *stream;
  25.        char    name[15];
  26.        int     numread;
  27.        char    inword = FALSE;
  28.        char    *gets();
  29.  
  30.        /* Get a filename */
  31.  
  32.        strcpy(name,"count.txt");
  33.  
  34.        /* Open file in binary mode     */
  35.  
  36.        if ((stream = fopen(name,"r")) == NULL)
  37.                return (1);
  38.  
  39.        /* Read file buffers, passing bytes read and inword status      */
  40.  
  41.        while ((numread = fread(buffer,1,BUFFSIZE,stream)) != 0)
  42.                inword = countwords(inword,numread);
  43.  
  44.        /* Calculate and print the results      */
  45.  
  46.        printf("\n\n\t\tFile statistics\n\n");
  47.        printf("\t\tBytes: %d \n",bytes);
  48.        printf("\t\tCharacters: %d \n",characters);
  49.        printf("\t\tLetters: %d \n",letters);
  50.        printf("\t\tVowels: %d \n",vowels);
  51.        printf("\t\tConsonants %d \n",letters - vowels);
  52.        printf("\t\tWords: %d \n",words);
  53.        printf("\t\tLines: %d \n",lines ? lines : 1);
  54.        printf("\t\tSentences: %d \n",sentences);
  55.        printf("\t\tWords per sentence: %-.1f \n",WPS);
  56.        printf("\t\tLetters per word: %-.1f \n",LPW);
  57.        printf("\t\tEstimated syllables per word: %-.1f \n",SPW);
  58.        return (0);
  59. }
  60.  
  61. /*
  62.  *  Analyze the chars in one buffer.
  63.  *
  64.  *  Increment bytes.  For each char, increment characters,
  65.  *  lines, and/or words if appropriate.  For each character,
  66.  *  call analyze function.  (A character is defined as a
  67.  *  printable ASCII code.)
  68.  */
  69.  
  70. countwords(inword,numread)
  71. char   inword;
  72. int    numread;
  73. {
  74.        int     count;
  75.        char    code;
  76.  
  77.        bytes += numread;
  78.        for (count = 0; count <= numread; ++count) {
  79.                code = buffer[count];
  80.                if (code == '\n')
  81.                        ++lines;
  82.                if (!inword) {
  83.                        if (code > ' ') {
  84.                                analyze(code,inword);
  85.                                inword = TRUE;
  86.                                ++words;
  87.                                ++characters;
  88.                        }
  89.                }
  90.                else {
  91.                        if (code <= ' ')
  92.                                inword = FALSE;
  93.                        else {
  94.                                ++characters;
  95.                                analyze(code,inword);
  96.                        }
  97.                }
  98.        }
  99.        return(inword);
  100. }
  101.  
  102. /*
  103.  *  Analyze a character.
  104.  *
  105.  *  Increment letters, vowels, and/or sentences
  106.  *  if appropriate.
  107.  */
  108.  
  109. analyze(code,inword)
  110. char   code;
  111. char   inword;
  112. {
  113.        if (isalpha(code)) {
  114.                ++letters;
  115.                if ((strchr("AEIOUaeiou",code)) || (strchr("Yy",code) && inword))
  116.                        ++vowels;
  117.        }
  118.        else {
  119.                if (strchr(".!?",code))
  120.                        ++sentences;
  121.        }
  122. }
  123.  
  124.